//
//  MessageViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2016/12/14.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "MessageViewController.h"
#import "LoginViewController.h"
#import "BenefitMsgViewController.h"
#import "ReplyListViewController.h"
#import "AnnouncementViewController.h"
#import "MessageCell.h"
#import "BulletModel.h"
#import "AdvanceNoticeModel.h"
#import "RewardModel.h"
#import "CommentNoticeModel.h"


@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *_advanceArray; //消息列表数组
    NSMutableArray *_bulletingArray;//公告消息数组
    NSMutableArray *_privateArray; //私信消息数组
    NSMutableArray *_leaveArray;    //留言消息
    NSMutableArray *_obtaintArray; //中奖消息数组
    NSMutableArray *_commentArray; //动态评论消息数组
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSString *userType;
@end

@implementation MessageViewController


#pragma mark -- 消息中心Request
- (void)messageCenterRequestWith:(NSString*)userType {
    NSString *starID;
    if ([userType isEqualToString:@"100"]) {//明星
        starID = User_ID;
    } else {//粉丝
        starID = @"";
    }
    NSDictionary *dict = @{
                           @"token":TOKEN,
                           @"udid":User_ID,
                           @"star":starID,
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kMsgCenter];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            if ([self.userType intValue] == 100) {//明星
               NSArray *advanceArr = responseObject[@"data"][@"advance"][@"list"];
                for (NSDictionary *advanceDict in advanceArr) {
                    AdvanceNoticeModel *model = [[AdvanceNoticeModel alloc] initWithDict:advanceDict];
                    [_advanceArray addObject:model];
                }
            } else {
                NSArray *obtainArr = responseObject[@"data"][@"obtain"][@"list"];
                for (NSDictionary *obtainDic in obtainArr) {
                    RewardModel *model = [[RewardModel alloc] initWithDict:obtainDic];
                    [_obtaintArray addObject:model];
                }
            }
            NSArray *bulletArr = responseObject[@"data"][@"bulletin"][@"list"];
            NSArray *privateArr = responseObject[@"data"][@"private"][@"list"];
            NSArray *leaveArr = responseObject[@"data"][@"leave"][@"list"];
            NSArray *commentArr = responseObject[@"data"][@"comment"][@"list"];

            for (NSDictionary *bulletDict in bulletArr) {
                BulletModel *model = [[BulletModel alloc] initWithDict:bulletDict];
                [_bulletingArray addObject:model];
            }

            for (NSDictionary *privateDic in privateArr) {
                CommentNoticeModel *model = [[CommentNoticeModel alloc] initWithDict:privateDic];
                [_privateArray addObject:model];
            }
            for (NSDictionary *leaveDic in leaveArr) {
                CommentNoticeModel *model = [[CommentNoticeModel alloc] initWithDict:leaveDic];
                [_leaveArray addObject:model];
            }
            for (NSDictionary *commentDict in commentArr) {
                CommentNoticeModel *model = [[CommentNoticeModel alloc] initWithDict:commentDict];
                [_commentArray addObject:model];
            }
        }
        [self.tableView reloadData];
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.userType = KString([[LSUserInfoManager sharedManager] currentUserInfo].u_type);
    self.navigationItem.title = @"消息中心";
    _advanceArray = [NSMutableArray arrayWithCapacity:1];
    _bulletingArray = [NSMutableArray arrayWithCapacity:1];
    _privateArray = [NSMutableArray arrayWithCapacity:1];
    _obtaintArray = [NSMutableArray arrayWithCapacity:1];
    _leaveArray = [NSMutableArray arrayWithCapacity:1];
    _commentArray = [NSMutableArray arrayWithCapacity:1];
    [self setUpUI];
    [self messageCenterRequestWith:self.userType];
}


#pragma mark -- UI
- (void)setUpUI {
    //tableView
    [self creatTableView];
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT - kNAV_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = kRGB(245, 245, 245, 1);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
}

#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 2;
            break;
        case 1:
            return 3;
            break;
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MessageCell *cell = [MessageCell loadNibCellWithTabelView:tableView];
    cell.MsgDate.hidden = YES;
    switch (indexPath.section) {
        case 0:{
            switch (indexPath.row) {
                case 0:{
                    cell.msgTitle.text = @"公告";
                    cell.MsgsenderIcon.image = kSET_IMAGE(@"Message_Notice");
                    if (_bulletingArray.count) {
                        BulletModel *model = _bulletingArray[0];
                        cell.messageDetailLabel.text = model.b_description;
                    }
                }
                    break;
                case 1:{
                    if ([self.userType intValue] == 100) {//明星
                        cell.msgTitle.text = @"主播消息";
                        cell.MsgsenderIcon.image = kSET_IMAGE(@"Message_LiveNotice");
                        if (_advanceArray.count) {
                            AdvanceNoticeModel *model = _advanceArray[0];
                            cell.messageDetailLabel.text = model.m_title;
                        } else {
                            cell.messageDetailLabel.text = @"暂无消息";
                        }
                    } else {//粉丝
                        cell.msgTitle.text = @"福利";
                        cell.MsgsenderIcon.image = kSET_IMAGE(@"Message_Benefit_");
                        if (_obtaintArray.count) {
                            cell.messageDetailLabel.text = @"恭喜您,您有新的中奖消息!";
                        } else {
                            cell.messageDetailLabel.text = @"暂无中奖消息";
                        }
                    }
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case 1:{
            switch (indexPath.row) {
                case 0:{
                    if (_privateArray.count) {
                        CommentNoticeModel *model = _privateArray[0];
                        cell.messageDetailLabel.text = model.c_content;
                        cell.MessageState.hidden = NO;
                    } else {
                        cell.messageDetailLabel.text = @"暂无";
                        cell.MessageState.hidden = YES;
                    }
                    
                    cell.msgTitle.text = @"私信";
                    cell.MsgsenderIcon.image = kSET_IMAGE(@"Message_PrivateLetter");
                    
                }
                    break;
                case 1:{
                    if (_leaveArray.count) {
                        CommentNoticeModel *model = _leaveArray[0];
                        cell.messageDetailLabel.text = model.c_content;
                        cell.MessageState.hidden = NO;
                    } else {
                        cell.messageDetailLabel.text = @"暂无";
                        cell.MessageState.hidden = YES;
                    }
                    cell.msgTitle.text = @"留言";
                    cell.MsgsenderIcon.image = kSET_IMAGE(@"Message_Leaveword");
                }
                    break;
                case 2:{
                    if (_commentArray.count) {
                        CommentNoticeModel *model = _commentArray[0];
                        cell.messageDetailLabel.text = model.c_content;
                        cell.MessageState.hidden = NO;
                    } else {
                        cell.messageDetailLabel.text = @"暂无";
                        cell.MessageState.hidden = YES;
                    }
                    cell.msgTitle.text = @"评论";
                    cell.MsgsenderIcon.image = kSET_IMAGE(@"Message_Comment");
                }
                    break;
                default:
                    break;
            }
        }
            break;
            
        default:
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 0:{
            switch (indexPath.row) {//公告
                case 0:{
                    AnnouncementViewController *announceVC = [[AnnouncementViewController alloc] init];
                    [self.navigationController pushViewController:announceVC animated:YES];
                }
                    break;
                case 1:{//福利
                    BenefitMsgViewController *beneVC = [[BenefitMsgViewController alloc] init];
                    [self.navigationController pushViewController:beneVC animated:YES];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case 1:{
            switch (indexPath.row) {
                case 0:{//私信
                    [self gotoReplyViewControllerWith:ReplyType_Private];
                }
                    break;
                case 1:{//留言
                    [self gotoReplyViewControllerWith:ReplyType_Leaveword];
                }
                    break;
                case 2:{//评论
                    [self gotoReplyViewControllerWith:ReplyType_Comment];
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark -- 区头视图

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 10)];
    headerView.backgroundColor = kRGB(245, 245, 245, 1);
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return 10;
}

#pragma mark -- 跳转回复列表
- (void)gotoReplyViewControllerWith:(ReplyType)replyType {
    ReplyListViewController *replylistVC = [[ReplyListViewController alloc] init];
    replylistVC.replyType = replyType;
    [self.navigationController pushViewController:replylistVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
